package org.item.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.item.common.bean.SysPermission;
import org.item.common.service.IPermissionService;
import org.item.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("permission")
public class PermissionController {
    @Autowired
    IPermissionService permissionService;

    @PostMapping("/")
    public ResultData createPermission(@RequestBody SysPermission permission) {
        SysPermission data = permissionService.insert(permission);
        return ResultData.success(data);
    }

    @PutMapping("/")
    public ResultData updatePermission(@RequestBody SysPermission permission) {
        SysPermission data = permissionService.update(permission);
        return ResultData.success(data);
    }

    @DeleteMapping("/{id}")
    public ResultData deletePermission(@PathVariable Integer id) {
        int result = permissionService.delete(id);
        if (result >= 1) {
            return ResultData.success(200, "权限删除成功", null);
        } else {
            return ResultData.fail();
        }
    }

    @GetMapping("/{id}")
    public ResultData getPermissionById(@PathVariable Integer id) {
        SysPermission data = permissionService.getById(id);
        return ResultData.success(data);
    }

    @GetMapping("/list")
    public ResultData getPermissionList(SysPermission permission) {
        return ResultData.success(permissionService.getList(permission));
    }

    @GetMapping("/page")
    public ResultData getPermissionByPage(IPage page, @RequestBody SysPermission permission) {
        IPage data = permissionService.getListPage(page, permission);
        return ResultData.success(data);
    }
}
